<?php
/**
 * @package Joomla
 * @subpackage Jmonoslideshow
 * @copyright (C) 2010 - Matthieu BARBE - www.ccomca.com
 * @license GNU/GPL v2
 * 
 * Jmonoslideshow is a Joomla extension for Monoslideshow.
 *
 * Jmonoslideshow uses :
 * Monoslideshow (http://www.monoslideshow.com/)
 * Plupload (http://www.plupload.com/) by Moxicode Systems
 * json class by Michal Migurski, Matt Knapp, Brett Stimmerman
 * jquery a JavaScript Library (http://jquery.com/)
 * getid3 library (http://getid3.sourceforge.net/)
 * swfobject is an easy-to-use and standards-friendly method to embed Flash content, which utilizes one small JavaScript file (http://code.google.com/p/swfobject/)
 * Chained Selects jQuery Plugin (http://www.appelsiini.net/2010/jquery-chained-selects)
 *
 * Jmonoslideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

// we use the static loadLanguage() method in the JPlugin class - a little tricky
	jimport('joomla.plugin.plugin');
	JPlugin::loadLanguage('com_jmonoslideshow', JPATH_ADMINISTRATOR);

if (version_compare(PHP_VERSION, '5.0.0', '<')) {
	// we add the component stylesheet to the installer
	$css = JURI::base().'components/com_jmonoslideshow/assets/monoslideshow.css'; 
	$document =& JFactory::getDocument(); 
	$document->addStyleSheet($css);	

	echo '<div class="alert">';
	echo JText::_( 'COM_JMONOSLIDESHOW_UPGRADE_PHP' ) . '<br />';
	echo '</div>';
}

jimport( 'joomla.filesystem.folder' );


	
	$folder[0][0]	=	'images' . DS . 'monoslideshow' . DS ;
	$folder[0][1]	= 	JPATH_ROOT . DS .  $folder[0][0];
	//clear a cache
	$cache = & JFactory::getCache();
	$cache->clean( null, 'com_jmonoslideshow' );
	
	
	
	$message = '';
	$error	 = array();
	foreach ($folder as $key => $value)
	{
		if (!JFolder::exists( $value[1]))
		{
			if (JFolder::create( $value[1], 0755 ))
			{
				@JFile::write($value[1].DS."index.html", "<html>\n<body bgcolor=\"#FFFFFF\">\n</body>\n</html>");
			//	@JFile::write($value[1].DS.".htaccess", "deny from all");
				$message .= '<p><b><span style="color:#009933">Folder</span> ' . $value[0] 
						   .' <span style="color:#009933">'.JText::_('COM_JMONOSLIDESHOW_CREATED').' !</span></b></p>';
				$error[] = 0;
			}	 
			else
			{
				$message .= '<p><b><span style="color:#CC0033">'.JText::_('COM_JMONOSLIDESHOW_FOLDER').'</span> ' . $value[0]
						   .' <span style="color:#CC0033">'.JText::_('COM_JMONOSLIDESHOW_CREATION_FAILED').' !</span></b>'.JText::_('COM_JMONOSLIDESHOW_PLEASE_CREATE_IT_MANUALLY').'.</p>';
				$error[] = 1;
			}
		}
		else//Folder exist
		{
			$message .= '<p><b><span style="color:#009933">'.JText::_('COM_JMONOSLIDESHOW_FOLDER').'</span> ' . $value[0] 
						   .' <span style="color:#009933">'.JText::_('COM_JMONOSLIDESHOW_EXIST').'!</span></b></p>';
			$error[] = 0;
		}
	}
	
	$status = new JObject();
	$status->modules = array();
	
	$modules = & $this->manifest->getElementByPath('modules');
if (is_a($modules, 'JSimpleXMLElement') && count($modules->children())) {

	foreach ($modules->children() as $module)
	{
		$mname		= $module->attributes('module');
		$mclient	= JApplicationHelper::getClientInfo($module->attributes('client'), true);

		// Set the installation path
		if (!empty ($mname)) {
			$this->parent->setPath('extension_root', $mclient->path.DS.'modules'.DS.$mname);
		} else {
			$this->parent->abort(JText::_('COM_JMONOSLIDESHOW_MODULE').' '.JText::_('COM_JMONOSLIDESHOW_INSTALL').': '.JText::_('COM_JMONOSLIDESHOW_NO_MODULE_SPECIFIED'));
			return false;
		}

		/*
		* If the module directory already exists, then we will assume that the
		* module is already installed or another module is using that directory.
		*/
		if (file_exists($this->parent->getPath('extension_root'))&&!$this->parent->getOverwrite()) {
			$this->parent->abort(JText::_('COM_JMONOSLIDESHOW_MODULE').' '.JText::_('COM_JMONOSLIDESHOW_INSTALL').': '.JText::_('COM_JMONOSLIDESHOW_ANOTHER_MODULE_IS_ALREADY_USING_DIRECTORY').': "'.$this->parent->getPath('extension_root').'"');
			return false;
		}

		// If the module directory does not exist, lets create it
		$created = false;
		if (!file_exists($this->parent->getPath('extension_root'))) {
			if (!$created = JFolder::create($this->parent->getPath('extension_root'))) {
				$this->parent->abort(JText::_('COM_JMONOSLIDESHOW_MODULE').' '.JText::_('COM_JMONOSLIDESHOW_INSTALL').': '.JText::_('COM_JMONOSLIDESHOW_FAILED_TO_CREATE_DIRECTORY').': "'.$this->parent->getPath('extension_root').'"');
				return false;
			}
		}

		/*
		* Since we created the module directory and will want to remove it if
		* we have to roll back the installation, lets add it to the
		* installation step stack
		*/
		if ($created) {
			$this->parent->pushStep(array ('type' => 'folder', 'path' => $this->parent->getPath('extension_root')));
		}

		// Copy all necessary files
		$element = &$module->getElementByPath('files');
		if ($this->parent->parseFiles($element, -1) === false) {
			// Install failed, roll back changes
			$this->parent->abort();
			return false;
		}

		// Copy language files
		$element = &$module->getElementByPath('languages');
		if ($this->parent->parseLanguages($element, $mclient->id) === false) {
			// Install failed, roll back changes
			$this->parent->abort();
			return false;
		}

		// Copy media files
		$element = &$module->getElementByPath('media');
		if ($this->parent->parseMedia($element, $mclient->id) === false) {
			// Install failed, roll back changes
			$this->parent->abort();
			return false;
		}

		$mtitle		= $module->attributes('title');
		$mposition	= $module->attributes('position');
		$morder		= $module->attributes('order');

		if ($mtitle && $mposition) {
			// if module already installed do not create a new instance
			$db =& JFactory::getDBO();
			$query = 'SELECT `id` FROM `#__modules` WHERE module = '.$db->Quote( $mname);
			$db->setQuery($query);
			if (!$db->Query()) {
				// Install failed, roll back changes
				$this->parent->abort(JText::_('COM_JMONOSLIDESHOW_MODULE').' '.JText::_('COM_JMONOSLIDESHOW_INSTALL').': '.$db->stderr(true));
				return false;
			}
			$id = $db->loadResult();

			if (!$id){
				$row = & JTable::getInstance('module');
				$row->title		= $mtitle;
				$row->ordering	= $morder;
				$row->position	= $mposition;
				$row->showtitle	= 0;
				$row->iscore	= 0;
				$row->access	= ($mclient->id) == 1 ? 2 : 0;
				$row->client_id	= $mclient->id;
				$row->module	= $mname;
				$row->published	= 1;
				$row->params	= '';

				if (!$row->store()) {
					// Install failed, roll back changes
					$this->parent->abort(JText::_('COM_JMONOSLIDESHOW_MODULE').' '.JText::_('COM_JMONOSLIDESHOW_INSTALL').': '.$db->stderr(true));
					return false;
				}
				
				// Make visible evertywhere if site module
				if ($mclient->id==0){
					$query = 'REPLACE INTO `#__modules_menu` (moduleid,menuid) values ('.$db->Quote( $row->id).',0)';
					$db->setQuery($query);
					if (!$db->query()) {
						// Install failed, roll back changes
						$this->parent->abort(JText::_('COM_JMONOSLIDESHOW_MODULE').' '.JText::_('COM_JMONOSLIDESHOW_INSTALL').': '.$db->stderr(true));
						return false;
					}
				}


			}


		}

		$status->modules[] = array('name'=>$mname,'client'=>$mclient->name);
	}
}


	
	
	$message .= '<p>'.JText::_('COM_JMONOSLIDESHOW_INSTALL_JMNOSLIDESHOW').'</p>';
	

	?>
	<div style="padding:20px;border:1px solid #b36b00;background:#fff">
		<a style="text-decoration:underline" href="http://jmonoslideshow.org" target="_blank"><?php
			echo  JHTML::_('image.site', 'jmonoslideshow.png', 'components/com_jmonoslideshow/assets/images/', NULL, NULL, 'Jmonoslideshow');
		?></a>
		<div style="position:relative;float:right;">
        <a style="text-decoration:underline" href="http://www.monoslideshow.com/" target="_blank">
			<?php echo  JHTML::_('image.site', 'logo.png', 'components/com_jmonoslideshow/assets/images/', NULL, NULL, 'Monoslideshow');?>
            </a>
		</div>
		<p>&nbsp;</p>
		<?php echo $message; ?>
        
        <?php if (count($status->modules)) : ?>
		<?php foreach ($status->modules as $module) : ?>
        <div class="approved">
		<?php echo sprintf ( JText::_( 'COM_JMONOSLIDESHOW_MODULE_INSTALLED' ),  $module['name']); ?> 
        </div>
		<?php endforeach;
		endif; ?>
        
		<div style="clear:both">&nbsp;</div>
		<div style="text-align:center"><center><table border="0" cellpadding="20" cellspacing="20">
			<tr>
				<td align="center" valign="middle">
					<a href="index.php?option=com_jmonoslideshow&amp;controller=jmonoslideshowinstall&amp;task=install"><?php
					echo JHTML::_('image.site',  'install.png', '/components/com_jmonoslideshow/assets/images/', NULL, NULL, 'Install' );
					?></a>
				</td>
				
				<td align="center" valign="middle">
					<a href="index.php?option=com_jmonoslideshow&amp;controller=jmonoslideshowinstall&amp;task=upgrade"><?php
					echo JHTML::_('image.site',  'upgrade.png', '/components/com_jmonoslideshow/assets/images/', NULL, NULL, 'Upgrade' );
					?></a>
				</td>
			</tr>
		</table></center></div>
		
		<p>&nbsp;</p><p>&nbsp;</p>
		<p>
		
		</p>
		
		<p>&nbsp;</p>
		<p><center><a style="text-decoration:underline" href="http://www.ccomca.ccom/" target="_blank"> <?php echo  JHTML::_('image.site', 'ccc.png', 'components/com_jmonoslideshow/assets/images/', NULL, NULL, 'CCC');?> Ccomca.ccom</a></center></p>		
